﻿using BaoGiaVeThang.DAL;
using BaoGiaVeThang.ServiceFlight;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaoGiaVeThang
{
    public class AirLineCrawler
    {
        public MonthInfo monthInfo;
        public DomesticRoute domesticRoute;
        public int indexDomesticRoute = 0;
        public int countTotal = 0, countError = 0,countNull=0;
        public AirLineCrawler(int startDay)
        {
            if (startDay == 29)
                startDay = 28;
            monthInfo = new MonthInfo(startDay);
            domesticRoute = ListDomesticRoute.getListDomesticRoute().First();
            indexDomesticRoute = 0;
        }
        public void Next()
        {
            indexDomesticRoute++;
            countTotal++;
            if (indexDomesticRoute >= ListDomesticRoute.getListDomesticRoute().Length)
            {
                monthInfo.month++;
                if (monthInfo.month > 12)
                    monthInfo.month = 1;
                indexDomesticRoute = 0;
            }
            domesticRoute = ListDomesticRoute.getListDomesticRoute()[indexDomesticRoute];
        }
        public virtual bool isDone()
        {
            return monthInfo.month == MonthInfo.startMonth-1;
        }
        public virtual DateTime getDate()
        {
            return DateTime.Now;
        }
        public void SaveLog(string name)
        {
            File.AppendAllText("log.txt",Environment.NewLine+string.Format("{0} :: Error: {1}; Null: {2}; Total: {3}",name,countError,countNull,countTotal));
        }
        public virtual async void UpdateCheapTicketPrice()
        {

        }
        public void UpdateToDatabase(RootMinPrice root, bool isVN, bool isVJ, bool isBL, DomesticRoute domesticRoute)
        {
            if(root==null||root.departurePrices==null||root.departurePrices.prices==null||root.departurePrices.prices.Count==0)
            {
                countNull++;
                return;
            }
            foreach (MinPriceDetail mpd in root.departurePrices.prices)
            {
                try
                {
                    if (mpd.Price == "" || mpd.Price == "0")
                        continue;
                    List<SqlParameter> sps = new List<SqlParameter>();
                    sps.Add(new SqlParameter("@DomesticRouteId", domesticRoute.Id));
                    sps.Add(new SqlParameter("@Date", mpd.Date.Day));
                    sps.Add(new SqlParameter("@Month", mpd.Date.Month));
                    if (isVN)
                    {
                        sps.Add(new SqlParameter("@VN", mpd.Price));
                    }
                    else if (isVJ)
                    {
                        sps.Add(new SqlParameter("@VJ", mpd.Price));
                    }
                    else if (isBL)
                    {
                        sps.Add(new SqlParameter("@BL", mpd.Price));
                    }
                    DBHelper.ExecuteNonQuery("InsertOrUpdateToMinPrice", sps);
                }
                catch(Exception ex)
                {
                }
            }
        }
    }
}
